<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\ApiuserRequest;
use App\Models\Apiuser;
use Illuminate\Http\Request;

class ApiuserController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if($request->wantsJson()){
            $where =[];
            $title = $request->input('username');
            if($title){
                $where =[['username', 'like', "%{$title}%"]];
            }
            $data  = $this->getPage('apiusers',$where);
            return response()->json($data);
        }
        return view('admin.apiuser.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.apiuser.create');
    }

    /**
     * 添加操作
     */
    public function store(ApiuserRequest $request)
    {
        //使用观察模型 操作数据 入库操
        $re = Apiuser::create($request->only('username','password'));
        if($re){
            return $this->reSuJson('添加成功！');
        }
        return $this->reErJson('添加失败');
    }
    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Apiuser  $apiuser
     * @return \Illuminate\Http\Response
     */
    public function show(Apiuser $apiuser)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Apiuser  $apiuser
     * @return \Illuminate\Http\Response
     */
    public function edit(Apiuser $apiuser)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Apiuser  $apiuser
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Apiuser $apiuser)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Apiuser  $apiuser
     * @return \Illuminate\Http\Response
     */
    public function destroy(Apiuser $apiuser)
    {
        $re = $apiuser->delete();
        if($re){
            return  $this->reSuJson('成功');
        }
        return  $this->reErJson('失败');
    }
}
